// Vue  项目的接口文件

var express = require("express")
var router = express.Router()

var {MovieModel,LiuYanModel,adminusers, adminannos, adminadvise} = require("../utils/model")
var axios  = require("axios")
var {createToken,checkToken} = require("../utils/token")
var moment = require("moment")
router.get("/test",(req,res)=>{
    res.json({
        code:200,
        msg:"test-test-api-api",
        result:null
    })
})


// 留言板 增删改查 
router.get('/liuyanlist',(req,res)=>{
    LiuYanModel.find({})
    .then(result=>{
        res.json({
            result,
            msg:'获取数据成功',
            code:200
        })
    })
    .catch(err=>{
        console.log(err)
        res.json({
            err,
            code:500,
            msg:"服务器异常",
            type:0,
        })
    })
})


// 留言板 添加  
router.post('/addliuyanone',(req,res)=>{
    var body = req.body;
    LiuYanModel.insertMany(body)
    .then(result=>{
        res.json({
            result,
            msg:'操作成功',
            code:200
        })
    })
    .catch(err=>{
        console.log(err)
        res.json({
            err,
            code:500,
            msg:"服务器异常",
            type:0,
        })
    })
})


// 留言板 删除  _id 
router.post('/removeliuyanone',(req,res)=>{
    var body = req.body;
    LiuYanModel.remove({
        _id:body._id
    })
    .then(result=>{
        res.json({
            result,
            msg:'删除成功',
            code:200
        })
    })
    .catch(err=>{
        console.log(err)
        res.json({
            err,
            code:500,
            msg:"服务器异常",
            type:0,
        })
    })
})

// 留言板修改  _id 
router.post('/updateliuyanone',(req,res)=>{
    var body = req.body;
    LiuYanModel.updateMany({
        _id:body._id
    },{
        $set:{
            title:body.title,
            content:body.content,
        }
    })
    .then(result=>{
        res.json({
            result,
            msg:'修改成功',
            code:200
        })
    })
    .catch(err=>{
        console.log(err)
        res.json({
            err,
            code:500,
            msg:"服务器异常",
            type:0,
        })
    })
})


router.get("/gettoken",(req,res)=>{
    res.json({
        code:200,
        msg:"获取token 成功",
        result:null
    })
})

// 登陆
router.post("/login",(req,res)=>{
    var body =req.body;
    delete body['remember']
    adminusers.findOne({
        ...body
    })
    .then(result=>{
        if(result){
            body.time =  new Date()
            var token = createToken(body)
            res.json({
                result,
                code:200,
                msg:"登陆成功",
                type:1,
                token
            })
        }else{
            res.json({
                code:402,
                msg:"登录失败,手机号码或者密码不正确",
                type:0,
            })
        }
    })
    .catch(err=>{
        console.log(err)
        res.json({
            err,
            code:500,
            msg:"服务器异常",
            type:0,
        })
    })
}) 


// 获取用户信息
router.get("/getuserinfo",(req,res)=>{
    checkToken(req,res,({
        phone
    })=>{
        adminusers.findOne({
            phone
        })
        .then(result=>{
            res.json({
                code:200,
                msg:"获取个人信息成功",
                type:1,
                result
            })
        })
        .catch(err=>{
            console.log(err)
            res.json({
                err,
                code:500,
                msg:"服务器异常",
                type:0,
            })
        })
    })
})


// 获取用户列表
router.get("/getuserlist",(req,res)=>{
    var {phone,role,username} = req.query;  
    console.log(req.query)
    var obj = {}
    // if(phone || username || role ){
    //     obj = {
    //         $or:[
    //             {
    //                 phone:new RegExp(phone),
    //             },
    //             {
    //                 username:new RegExp(username),
    //             },
    //             {
    //                 role:role  * 1 
    //             }
    //         ]
    //     }
    // }
    if(username){
        // obj.push({username:new RegExp(username)})
        obj = {
            ...obj,
            username:new RegExp(username)
        }
    }
    if(phone){
        obj = {
            ...obj,
            phone:new RegExp(phone)
        }
        // obj.push({phone:new RegExp(phone)})
    }
    if(role){
        obj = {
            ...obj,
            role:role*1
        }
    }

    // var search = {

    // }
    // if(obj.length>0){
    //     search = {$or:obj}
    // }


    checkToken(req,res,({
        phone
    })=>{
        adminusers.find(obj)
        .sort({
            _id:-1
        })
        .then(result=>{
            res.json({
                code:200,
                msg:result.length>0?"获取用户列表成功":"暂无数据",
                type:1,
                result
            })
        })
        .catch(err=>{
            console.log(err)
            res.json({
                err,
                code:500,
                msg:"服务器异常",
                type:0,
            })
        })
    })
})



// 发送验证码 
router.get("/sendcaptcha",(req,res)=>{
    var {phone} = req.query;
    adminusers.findOne({
        phone:phone
    })
    .then(data=>{
        if(data){
            // 发送验证码
            axios.get("http://47.104.209.44:3333/captcha/sent",{
                params:{
                   phone:phone 
                }
            })
            .then(result=>{
                res.json({
                    code:200,
                    msg:"验证码发送成功",
                    type:1,
                    ...result.data
                })
            })
        }else{
            res.json({
                code:402,
                msg:"手机号码暂未注册,请联系管理员",
                type:0,
            })
        }
    })
    .catch(err=>{
        console.log(err)
        res.json({
            err,
            code:500,
            msg:"服务器异常",
            type:0,
        })
    })
})

// 校验验证码
router.get("/verifycaptcha",(req,res)=>{
    var {phone,captcha} = req.query;
    adminusers.findOne({
        phone:phone
    })
    .then(data=>{
        if(data){
            // 校验验证码
            axios.get("http://47.104.209.44:3333/captcha/verify",{
                params:{
                   phone:phone ,
                   captcha
                }
            })
            .then(result=>{
                var token = createToken({phone,time:new Date()})
                res.json({
                    code:200,
                    msg:"验证码验证成功",
                    type:1,
                    ...result.data,
                    token
                })
            })
        }else{
            res.json({
                code:402,
                msg:"手机号码暂未注册,请联系管理员",
                type:0,
            })
        }
    })
    .catch(err=>{
        console.log(err)
        res.json({
            err,
            code:500,
            msg:"服务器异常",
            type:0,
        })
    })
})


// 添加用户
router.post("/addusers",(req,res)=>{
    const body = req.body;
    checkToken(req,res,({
        phone
    })=>{
        adminusers.findOne({
            phone:body.phone
        }).then(data=>{
            if(data){
                res.json({
                    result:null,
                    msg:"添加失败,手机号码已经被添加",
                    code:402,
                    type:0
                })
            }else{
                body.time = new Date();
                adminusers.insertMany(body)
                .then(result=>{
                    res.json({
                        result,
                        msg:"添加用户成功",
                        code:200,
                        type:1
                    })
                })
            }
        }) 
    })
})



// 删除用户
router.post("/deluserone",(req,res)=>{
    const body = req.body;
    checkToken(req,res,({
        phone
    })=>{
        adminusers.remove({
            _id:body._id
        })
        .then(result=>{
            res.json({
                result,
                msg:'删除成功',
                code:200
            })
        })
        .catch(err=>{
            console.log(err)
            res.json({
                err,
                code:500,
                msg:"服务器异常",
                type:0,
            })
        })
    })
})

//修改用户
router.post("/updateuserone",(req,res)=>{
    const body = req.body;
    checkToken(req,res,({
        phone
    })=>{
        adminusers.updateMany({
            _id:body._id
        },{
            $set:body
        })
        .then(result=>{
            res.json({
                result,
                msg:'修改成功',
                code:200
            })
        })
        .catch(err=>{
            console.log(err)
            res.json({
                err,
                code:500,
                msg:"服务器异常",
                type:0,
            })
        })
    })
})

// 上传文件 
var multer = require("multer")
var storage = multer.diskStorage({   // 硬盘存储 
    destination: function (req, file, cb) {
      cb(null, './public/avatar')    // 文件路径 
    },
    filename: function (req, file, cb) {  // 文件名 
      cb(null, Date.now()+ "WH2010"+file.originalname)  
    }
})
var upload = multer({ storage: storage }).any()  // 不限定文件 数量

router.post("/uploadavatar",upload,(req,res)=>{
    checkToken(req,res,({phone})=>{
       
        var path = req.files[0].path;
        adminusers.updateMany({
            phone,
        },{
            $set:{
                avatar:path
            }
        }).then(resutl=>{
            res.json({
                code:200,
                msg:"上传成功",
                avatar:path,
                type:1,
                resutl
            })
        }).catch(err=>{
            console.log(err)
            res.json({
                err,
                code:500,
                msg:"服务器异常",
                type:0,
            })
        })
        
    })
})



// 上传文件图片 
router.post("/uploadfileimg",upload,(req,res)=>{
    checkToken(req,res,({phone})=>{
        var path = req.files[0].path;
        res.json({
            code:200,
            msg:"上传成功",
            result:path,
            type:1,
        })
    })
})


// 新增 
router.post("/addannoone",(req,res)=>{
    const body = req.body;
    checkToken(req,res,({
        phone
    })=>{
        adminusers.findOne({
            phone
        })
        .then(data=>{
            body.time = new Date()
            body.author = data.username
            body.avatar = data.avatar 
            adminannos.insertMany(body)
            .then(result=>{
                res.json({
                    result,
                    msg:'添加通告成功',
                    code:200,
                    type:1
                })
            })
            .catch(err=>{
                console.log(err)
                res.json({
                    err,
                    code:500,
                    msg:"服务器异常",
                    type:0,
                })
            })
        })
    })
})


// 查询 
router.post("/findannolist",(req,res)=>{
    var {author,category,time} = req.body;  
    var obj = {}
    console.log(time)
    if(author){
        obj = {
            ...obj,
            author:new RegExp(author)
        }
    }
    if(category){
        obj = {
            ...obj,
            category:category.toString()
        }
    }

    if(time){
        var start=(moment(time[0]).format('YYYY-MM-DD')).substring(0,16).replace('T',' ').replace(/-/g,'-')
        var end=(moment(time[1]).format('YYYY-MM-DD')).substring(0,16).replace('T',' ').replace(/-/g,'-')
        obj = {
            ...obj,
            time:{
                $gte:start,     // 时间需要转换 
                $lte:end
            }
        }
    }


    checkToken(req,res,({
        phone
    })=>{
        adminannos.find(obj)
        .sort({
            _id:-1
        })
        .then(result=>{
            res.json({
                result,
                msg:'查询成功',
                code:200,
                type:1
            })
        })
        .catch(err=>{
            console.log(err)
            res.json({
                err,
                code:500,
                msg:"服务器异常",
                type:0,
            })
        })
    })
})


// 删除通告 
router.post("/delannoone",(req,res)=>{
    const body = req.body;
    checkToken(req,res,({
        phone
    })=>{
        adminannos.remove({
            _id:body._id
        })
        .then(result=>{
            res.json({
                result,
                msg:'删除成功',
                code:200
            })
        })
        .catch(err=>{
            console.log(err)
            res.json({
                err,
                code:500,
                msg:"服务器异常",
                type:0,
            })
        })
    })
})

// 查询通告详情 
router.post("/getannoone",(req,res)=>{
    const body = req.body;
    checkToken(req,res,({
        phone
    })=>{
        adminannos.findOne({
            _id:body._id
        })
        .then(result=>{
            res.json({
                result,
                msg:'查询成功',
                code:200
            })
        })
        .catch(err=>{
            console.log(err)
            res.json({
                err,
                code:500,
                msg:"服务器异常",
                type:0,
            })
        })
    })
})

// 修改通告 
router.post("/updateannoone",(req,res)=>{
    const body = req.body;
    checkToken(req,res,({
        phone
    })=>{
        adminannos.updateMany({
            _id:body._id
        },{
            $set:body
        })
        .then(result=>{
            res.json({
                result,
                msg:'修改成功',
                code:200
            })
        })
        .catch(err=>{
            console.log(err)
            res.json({
                err,
                code:500,
                msg:"服务器异常",
                type:0,
            })
        })
    })
})


// 新增 
router.post("/addadviseone",(req,res)=>{
    const body = req.body;
    checkToken(req,res,({
        phone
    })=>{
        adminusers.findOne({
            phone
        })
        .then(data=>{
            body.time = new Date()
            body.author = data.username
            body.avatar = data.avatar 
            adminadvise.insertMany(body)
            .then(result=>{
                res.json({
                    result,
                    msg:'添加成功',
                    code:200,
                    type:1
                })
            })
            .catch(err=>{
                console.log(err)
                res.json({
                    err,
                    code:500,
                    msg:"服务器异常",
                    type:0,
                })
            })
        })
    })
})



// 查询 
router.post("/findadviselist",(req,res)=>{
    var {author,category,time} = req.body;  
    var obj = {}
    if(author){
        obj = {
            ...obj,
            author:new RegExp(author)
        }
    }
    if(category){
        obj = {
            ...obj,
            category:category
        }
    }
    if(time){
        var start=(moment(time[0]).format('YYYY-MM-DD')).substring(0,16).replace('T',' ').replace(/-/g,'-')
        var end=(moment(time[1]).format('YYYY-MM-DD')).substring(0,16).replace('T',' ').replace(/-/g,'-')
        obj = {
            ...obj,
            time:{
                $gte:start,     // 时间需要转换 
                $lte:end
            }
        }
    }
    checkToken(req,res,({
        phone
    })=>{
        adminadvise.find(obj)
        .sort({
            _id:-1
        })
        .then(result=>{
            res.json({
                result,
                msg:'查询成功',
                code:200,
                type:1
            })
        })
        .catch(err=>{
            console.log(err)
            res.json({
                err,
                code:500,
                msg:"服务器异常",
                type:0,
            })
        })
    })
})

// 删除 意见 
router.post("/deladviseone",(req,res)=>{
    const body = req.body;
    checkToken(req,res,({
        phone
    })=>{
        adminadvise.remove({
            _id:body._id
        })
        .then(result=>{
            res.json({
                result,
                msg:'删除成功',
                code:200
            })
        })
        .catch(err=>{
            console.log(err)
            res.json({
                err,
                code:500,
                msg:"服务器异常",
                type:0,
            })
        })
    })
})



// 
router.post("/getadviseone",(req,res)=>{
    const body = req.body;
    checkToken(req,res,({
        phone
    })=>{
        adminadvise.findOne({
            _id:body._id
        })
        .then(result=>{
            res.json({
                result,
                msg:'查询成功',
                code:200
            })
        })
        .catch(err=>{
            console.log(err)
            res.json({
                err,
                code:500,
                msg:"服务器异常",
                type:0,
            })
        })
    })
})

//  
router.post("/updateadviseone",(req,res)=>{
    const body = req.body;
    checkToken(req,res,({
        phone
    })=>{
        adminadvise.updateMany({
            _id:body._id
        },{
            $set:body
        })
        .then(result=>{
            res.json({
                result,
                msg:'修改成功',
                code:200
            })
        })
        .catch(err=>{
            console.log(err)
            res.json({
                err,
                code:500,
                msg:"服务器异常",
                type:0,
            })
        })
    })
})

module.exports = router